/*
 * InfosetResult.java
 *
 * Created on September 5, 2006, 4:19 PM
 *
 * (C) 2006 R. Alexander Milowski
 */

package org.infoset.xml.jaxp;

import java.net.URI;
import javax.xml.transform.sax.SAXResult;
import org.infoset.xml.Document;
import org.infoset.xml.Item;
import org.infoset.xml.ItemDestination;
import org.infoset.xml.XMLException;
import org.infoset.xml.sax.SAXEventGenerator;
import org.xml.sax.ContentHandler;

/**
 *
 * @author alex
 */
public class InfosetResult extends SAXResult {

   ItemDestination dest;
   SAXEventGenerator generator;
   
   public InfosetResult() {
      this(null);
   }
   
   /** Creates a new instance of InfosetResult */
   public InfosetResult(ItemDestination itemDest) {
      this.dest = itemDest;
      generator = new SAXEventGenerator();
      generator.setItemDestination(new ItemDestination() {
         public void send(Item item)
            throws XMLException
         {
            if (item.getType()==Item.ItemType.DocumentItem) {
               Document doc = (Document)item;
               URI baseURI = doc.getBaseURI();
               String systemId = getSystemId();
               if (baseURI==null && systemId!=null) {
                  doc.setBaseURI(URI.create(systemId));
               }
            }
            dest.send(item);
         }
      });
   }
   
   public void setItemDestination(ItemDestination dest) {
      this.dest = dest;
   }
   
   public void setHandler(ContentHandler handler) {
      throw new UnsupportedOperationException("The setHandler() method cannot be called on "+this.getClass().getName());
   }
   
   public ContentHandler getHandler() {
      return generator;
   }

}
